package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.alimama.mobile.sdk.config.system.MMLog;
import com.xm9m.ibczy.mytest.R;
import com.xm9m.ibczy.mytest.alimama.mobile.demo.model.StyleItem;

import java.util.ArrayList;
import java.util.List;

//import com.alimama.mobile.sdk.hack.InstrumentationHook;

public class MainActivity extends Activity {

	private ListView styleLv;
	private List<StyleItem> styles;
	private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inflater = getLayoutInflater();
        
        styles = new ArrayList<StyleItem>();
        styles.add(new StyleItem(R.drawable.ic_handler, "推广墙", HandleActivity.class));
        styles.add(new StyleItem(R.drawable.ic_banner, "横幅", BannerActivity.class));
        styles.add(new StyleItem(R.drawable.ic_fullscreen, "插屏",InsertActivity.class));
        styles.add(new StyleItem(R.drawable.ic_fullscreen, "video插屏",VideoInsertActivity.class));
        styles.add(new StyleItem(R.drawable.ic_feeds, "信息流", FeedViewActivity.class));
        styles.add(new StyleItem(R.drawable.ic_text, "文字链", TextLinkActivity.class));
        styles.add(new StyleItem(R.drawable.ic_welcome, "开屏", WelcomeActivity.class));
        styles.add(new StyleItem(R.drawable.ic_loop, "轮播大图", LoopImageActivity.class));
        styles.add(new StyleItem(R.drawable.ic_loop, "轮播大图数据接口方式", LoopImageDataInterfaceActivity.class));
        styles.add(new StyleItem(R.drawable.ic_container, "内嵌", ContainerActivity.class));
        
        ((TextView) findViewById(R.id.title)).setText("样式列表");
        ((ImageButton) findViewById(R.id.back)).setVisibility(View.GONE);
        styleLv = (ListView) findViewById(R.id.styles);
        
        StyleAdapter adapter = new StyleAdapter();
        styleLv.setAdapter(adapter);
        styleLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				StyleItem item = (StyleItem)parent.getItemAtPosition(position);
				if(item != null){
					Intent intent = new Intent(MainActivity.this, item.getActivityClass());
					startActivity(intent);
				}
			}
		});
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MMLog.i("######### "+requestCode+"  "+resultCode);
    }
    
    class StyleAdapter extends BaseAdapter {
    	
		@Override
		public int getCount() {
			return styles.size();
		}

		@Override
		public StyleItem getItem(int position) {
			return styles.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView == null) {
	            convertView = inflater.inflate(R.layout.style_item, null);
	            holder = new ViewHolder();
	            holder.tv = (TextView)convertView.findViewById(R.id.style_item);
	            convertView.setTag(holder);
	        } else {
	            holder = (ViewHolder) convertView.getTag();
	        }
	        holder.tv.setText(styles.get(position).getStyleName());
	        holder.tv.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(styles.get(position).getStyleIcon()), null, null, null);
	        return convertView;
		}
    	
    }
    
    static class ViewHolder{
        private TextView tv;
    }
}
